package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.pet.dao.PetReportDao;
import io.renren.modules.pet.dto.PetLostDTO;
import io.renren.modules.pet.service.PetLostService;
import io.renren.modules.pet.vo.PetReportVO;
import io.renren.modules.pet.dto.PetReportDTO;
import io.renren.modules.pet.entity.PetReportEntity;
import io.renren.modules.pet.service.PetReportService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 * 举报表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-14
 */
@Service
public class PetReportServiceImpl extends CrudServiceImpl<PetReportDao, PetReportEntity, PetReportVO> implements PetReportService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PetLostService petLostService;

    @Override
    public QueryWrapper<PetReportEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PetReportEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.orderByDesc("create_date");

        return wrapper;
    }

    @Override
    public void save(PetReportDTO petReportDTO) {
        PetReportEntity petReportEntity = ConvertUtils.sourceToTarget(petReportDTO, PetReportEntity.class);
        this.insert(petReportEntity);
    }

    @Override
    public void update(PetReportDTO petReportDTO) {
        PetReportEntity petReportEntity = ConvertUtils.sourceToTarget(petReportDTO, PetReportEntity.class);
        this.updateById(petReportEntity);
    }

    @Override
    public PageData<PetReportVO> petReportPage(Map<String, Object> params) {
        PageData<PetReportVO> page = this.page(params);
        page.getList().forEach(e -> {
            // 被举报人姓名
            SysUserDTO sysUserDto = sysUserService.get(e.getReportedUserId());
            e.setReportedUserName(sysUserDto.getRealName());
            // 照片集合
            e.setImageUrlList(Arrays.asList(e.getImageUrl().split(",")));
            e.setImageUrlSize(e.getImageUrlList().size());
            // 举报人姓名
            e.setCreatorName(sysUserService.get(e.getCreator()).getRealName());
        });
        return page;
    }

    @Override
    public void examine(PetReportDTO dto) {
        this.update(dto);
        PetReportEntity reportEntity = this.selectById(dto.getId());
        if (dto.getStatus() == 1) {
            // 审核通过，下架被举报人寻宠信息
            petLostService.examineByUserId(reportEntity.getReportedUserId(), 2);
        } else {
            petLostService.examineByUserId(reportEntity.getReportedUserId(), 1);
        }
    }

}